package edu.uj.petri.model;

import edu.uj.petri.commands.Memento;

/**
 * @author Piotrek
 */
class PlaceObjectImpl extends PlaceImpl {
    private int tokenCount;

    private String name;

    private ObjectModel model;

    public PlaceObjectImpl(ObjectModel model) {
        this.model = model;
    }

    public void addTokens(int number) {
        tokenCount += number;
        model.modelChanged();
    }

    public void deleteTokens(int number) {
        addTokens(-number);
    }

    public void setTokens(int number) {
        tokenCount = number;
        model.modelChanged();
    }

    public int getTokens() {
        return tokenCount;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    /** stores internal state in a memento */
    public Memento getMemento() {
        PlaceObjectImplMemento retval = new PlaceObjectImplMemento();
        retval.name = this.name;
        retval.tokenCount = this.tokenCount;
        return retval;
    }

    /**
     * restores internal state of object to state stored in the memento
     */
    public void applyMemento(Memento memento) {
        PlaceObjectImplMemento mem;
        try {
            mem = (PlaceObjectImplMemento)memento;
        } catch(ClassCastException e) {
            throw new IllegalArgumentException(e.getLocalizedMessage());
        }
        this.name = mem.name;
        this.tokenCount = mem.tokenCount;
        model.modelChanged();
    }
    
    
    static class PlaceObjectImplMemento extends Memento {
        String name;
        int tokenCount;
    }
    
    private static final long serialVersionUID = 6345543229919242299L;
}
